//
//  JDNewSaleCollectionViewCell.swift
//  DragonWallet
//
//  Created by volantPig on 2025/9/14.
//

import UIKit

class JDNewSaleCollectionViewCell: UICollectionViewCell {
    
    lazy var containerView: UIView = {
        let container = UIView()
        container.makeCorner(4)
        container.backgroundColor = .white
        return container
    }()
    
    
    lazy var goodsImageV: UIImageView = {
        let goodsImg = UIImageView()
        return goodsImg
    }()
    
    lazy var goodsNameLabel: UILabel = {
        let goodsName = UILabel()
        goodsName.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 15, weight: .regular), textColor: color("#333333"), numberOfLines: 2)
        return goodsName
    }()
    
    lazy var countryInfView: JDCountryInfoView = {
        let countryInfo = JDCountryInfoView()
        return countryInfo
    }()
    
//    lazy var countryImageV: UIImageView = {
//        let countryImg = UIImageView()
//        
//        return countryImg
//    }()
//    
//    lazy var countryNameLabel: UILabel = {
//        let countryName = UILabel()
//        countryName.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 10, weight: .regular), textColor: color("#333333"), numberOfLines: 1)
//        return countryName
//    }()
//    
    
    
    lazy var priceLabel: UILabel = {
        let price = UILabel()
        price.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 20, weight: .semibold), textColor: color("#333333"), numberOfLines: 1)
        return price
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupUIElements()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUIElements() {
        self.contentView.addSubview(self.containerView)
        self.containerView.snp.makeConstraints { make in
            make.leading.trailing.top.bottom.equalTo(self.contentView)
        }
        
        self.containerView.addSubview(self.goodsImageV)
        self.goodsImageV.snp.makeConstraints { make in
            make.leading.trailing.top.equalTo(self.containerView)
            make.height.equalTo(self.goodsImageV.snp.width)
        }

        self.containerView.addSubview(self.goodsNameLabel)
        self.goodsNameLabel.snp.makeConstraints { make in
            make.leading.equalTo(self.containerView).offset(8)
            make.trailing.equalTo(self.containerView).offset(-8)
            make.top.equalTo(self.goodsImageV.snp.bottom).offset(13)
            make.height.greaterThanOrEqualTo(42)
        }
        self.goodsNameLabel.text = "新品日系TANKER 零钱卡包"
        
        self.containerView.addSubview(self.countryInfView)
        self.countryInfView.snp.makeConstraints { make in
            make.leading.equalTo(self.containerView).offset(8)
            make.top.equalTo(self.goodsNameLabel.snp.bottom).offset(7)
        }
        
        
        
//        self.containerView.addSubview(self.countryImageV)
//        self.countryImageV.snp.makeConstraints { make in
//            make.leading.equalTo(self.containerView).offset(8)
//            make.size.equalTo(CGSizeMake(20, 20))
//            make.top.equalTo(self.goodsNameLabel.snp.bottom).offset(7)
//        }
//        self.countryImageV.backgroundColor = .red
//        
//        
//        self.containerView.addSubview(self.countryNameLabel)
//        self.countryNameLabel.snp.makeConstraints { make in
//            make.leading.equalTo(self.countryImageV.snp.trailing).offset(3)
//            make.centerY.equalTo(self.countryImageV)
//        }
        
        
        self.contentView.addSubview(self.priceLabel)
        self.priceLabel.snp.makeConstraints { make in
            make.leading.equalTo(self.containerView).offset(8)
            make.top.equalTo(self.countryInfView.snp.bottom).offset(10)
            make.height.equalTo(28)
        }
        self.priceLabel.text = "￥20.00"
        
    }
    
    func updateDate(_ model: JDGoodsModel?) {
//        {"origin":"泰国","goodsName":"商品测试03","goodsId":77,"categoryId":155,"goodsPriceText":"600.00","goodsPrice":600000,"brand":"品牌","countryCode":"TH","goodsMainImgUrl":"018f73e0110c96a09a7b81e54cd199baf60f42101ab7cf1d4904a0386f6ff85f"}
        
        self.goodsNameLabel.text = model?.goodsName
        self.priceLabel.text = String(format: "￥%@", model?.goodsPriceText ?? "0.00")
        self.priceLabel.priceString(UIFont.systemFont(ofSize: 12, weight: .regular), UIFont.systemFont(ofSize: 20, weight: .semibold))
        
        self.countryInfView.countryNameLabel.text = model?.origin
        
        // JP: 日本  KR: 韩国  TH: 泰国
        if model?.countryCode == "JP" {
            self.countryInfView.countryImageV.image = UIImage(named: "home_goods_country_jp")
            
        } else if model?.countryCode == "KR" {
            self.countryInfView.countryImageV.image = UIImage(named: "home_goods_country_kr")
        } else if model?.countryCode == "TH" {
            self.countryInfView.countryImageV.image = UIImage(named: "home_goods_country_th")
        }
        
//        self.countryNameLabel.text = model?.origin
        self.goodsImageV.downloadImage(model?.goodsMainImgUrl ?? "")
    }
    
}

class JDCountryInfoView: UIView {
    lazy var countryImageV: UIImageView = {
        let countryImage = UIImageView()
        countryImage.makeCorner(10)
        return countryImage
    }()
    
    lazy var countryNameLabel: UILabel = {
        let countryName = UILabel()
        countryName.labelConfig(textAlignment: .left, font: UIFont.systemFont(ofSize: 10, weight: .regular), textColor: color("#333333"), numberOfLines: 1)
        return countryName
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = color("#F8F8F8")
        self.setupUIElements()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUIElements() {
        
        self.makeCorner(10)
        
        self.addSubview(self.countryImageV)
        self.countryImageV.snp.makeConstraints { make in
            make.leading.top.bottom.equalTo(self)
            make.size.equalTo(CGSize(width: 20, height: 20))
        }
        
        self.addSubview(self.countryNameLabel)
        self.countryNameLabel.snp.makeConstraints { make in
            make.leading.equalTo(self.countryImageV.snp.trailing).offset(3)
            make.trailing.equalTo(self).offset(-6)
            make.centerY.equalTo(self)
        }
        self.countryNameLabel.text = "日本"
        
        
    }
    
}
